/**
 * 
 */
package cn.panda.edu.teacher;

import cn.panda.edu.model.Teacher;
import cn.panda.edu.service.TeacherService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.MathUtil;
import cn.panda.edu.util.ResultDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**  
 * 类描述：  
 */
@RestController
@RequestMapping(value="api/teacher")
@Api(description="教师登陆")
public class TeacherLoginController {
	@Autowired
	private TeacherService teacherService;
	
	@ApiOperation(value ="教师登陆", notes ="")
	@ApiImplicitParam(name ="teacher", value ="教师类", paramType ="form")
	@RequestMapping(value="/login",method= RequestMethod.POST)
	public Map<String, Object> manageLogin(String teacherRealname, String teacherPwd, HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
//		String pwd = MathUtil.getMD5(teacherId.toString()+"#"+teacherPwd);
//		System.out.println(teacherId+"---"+pwd);
		Teacher teacher = teacherService.teacherLogin(teacherRealname,teacherPwd);
		System.out.println(teacher);
		if (teacher != null) {
			request.getSession().setAttribute("teacher", teacher);
			return ResultDataUtil.resData(200, "登陆成功！", teacher);
		}else {
			return ResultDataUtil.resData(500, "账号密码错误！", null);
		}
		
		
	}
	@ApiOperation(value ="教师修改密码", notes ="")
	@ApiImplicitParam(name ="teacher", value ="教师类", paramType ="form")
	@RequestMapping(value="/modfiyPwd",method= RequestMethod.POST)
	public Map<String, Object> modfiyPwd(Integer teacherId, String teacherPwd, HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
		String pwd = MathUtil.getMD5(teacherId.toString()+"#"+teacherPwd);
		System.out.println(teacherId+"---"+pwd);
		int num =0;
		num=teacherService.teacherModfiyPwd(teacherId, pwd);
		//System.out.println(teacher);
		if (num>0) {
			//request.getSession().setAttribute(Constant.LOGIN_USER, teacher);
			return ResultDataUtil.resData(200, "修改成功！", null);
		}else {
			return ResultDataUtil.resData(500, "修改失败，请重试！", null);
		}
		
		
	}
	@ApiOperation(value ="教师退出", notes ="")
	@RequestMapping(value="/logout",method= RequestMethod.POST)
	public Map<String, Object> manageLogin(HttpSession session){
		session.removeAttribute(Constant.LOGIN_USER);
		return ResultDataUtil.resData(200, "账号已退出！", null);
	}
}
